package com.tjnu.losca.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjnu.losca.mapper.GradeMapper;
import com.tjnu.losca.mapper.StudentbygradeMapper;
import com.tjnu.losca.pojo.Grade;
import com.tjnu.losca.pojo.Studentbygrade;
import com.tjnu.losca.service.IGradeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 班级 服务实现类
 * </p>
 *
 * @author losca
 * @since 2021-10-05
 */
@Service
public class GradeServiceImpl extends ServiceImpl<GradeMapper, Grade> implements IGradeService {

    @Autowired
    private StudentbygradeMapper studentbygradeMapper;

    @Override
    public boolean check(Integer id, String value) {
        QueryWrapper<Grade> gradeQueryWrapper = new QueryWrapper<>();
        gradeQueryWrapper.eq("name", value);
        if (id != null) {
            gradeQueryWrapper.ne("id", id);
        }
        return this.count(gradeQueryWrapper) == 0;
    }

    @Override
    public IPage getDataByName(int pageNum, int pageSize, String name) {
        QueryWrapper<Grade> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(name)){
            wrapper.like("name",name);
        }
        return this.page(new Page<>(pageNum,pageSize),wrapper);
    }

    @Override
    public boolean del(Integer id) {
        Grade grade = this.getById(id);
        //状态设置为1为逻辑删除
        grade.setState(1);
        return this.updateById(grade);
    }

    @Override
    public boolean recovery(Integer id) {
        Grade grade = this.getById(id);
        //状态设为0恢复上课状态
        grade.setState(0);
        return this.updateById(grade);
    }

    @Override
    public IPage getDetails(Integer pageNum,Integer pageSize,Integer id) {
        QueryWrapper<Studentbygrade> wrapper = new QueryWrapper<>();
        wrapper.eq("gradeId",id);
        Page<Studentbygrade> page = new Page<>(pageNum,pageSize);
        return studentbygradeMapper.selectPage(page,wrapper);
    }
}
